<!-- 联级菜单 -->
 <script setup>
 import { ref, onMounted } from 'vue';
 import { useRouter } from 'vue-router'
 const router = useRouter()
const props = defineProps({
    // 菜单数据
    menuDataSource: {
        type: Array,
        default: () => [
            {
                tabIcon: 'https://so.360tres.com/sdm/242_150_/t014e90eac42c10e64f.webp',
                tabName: '一级标题',
                children: [
                    { tabName: '二级标题', isChose: true },
                    { tabName: '二级标题', isChose: false },
                    { tabName: '二级标题', isChose: false }
                ]
            },
            {
                tabIcon: 'https://so.360tres.com/sdm/242_150_/t014e90eac42c10e64f.webp',
                tabName: '一级标题',
                children: [
                    { tabName: '二级标题', isChose: false },
                    { tabName: '二级标题', isChose: false }
                ]
            },
            {
                children: [
                    { tabName: '二级标题', isChose: false, tabIcon: 'https://so.360tres.com/sdm/242_150_/t014e90eac42c10e64f.webp' },
                ]
            }
        ]
    }
});
const menuData = ref(props.menuDataSource);

// 获取到路由路径，给当前路径的菜单添加选中状态
onMounted(() => {
    const currentPath = router.currentRoute.value.path;
    menuData.value.forEach(parent => {
        parent.children.forEach(child => {
            if(child.path === currentPath) {
                child.isChose = true;
            } else {
                child.isChose = false;
            }
        });
    });
});

// 监听路由变化，更新菜单选中状态
router.afterEach((to, from) => {
    const currentPath = to.path;
    menuData.value.forEach(parent => {
        parent.children.forEach(child => {
            if(child.path === currentPath) {
                child.isChose = true;
            } else {
                child.isChose = false;
            }
        });
    });
});

const menuClick = (item) => {
    // 重置所有的isChose为false
    menuData.value.forEach(parent => {
        parent.children.forEach(child => {
            child.isChose = false;
        });
    });
    item.isChose = true;
    if(item.path) {
        router.push(item.path)
    }
};
</script>

<template>
    <div class="level-menu">
        <div class="menu-item" v-for="(item, index) in menuData" :key="index">
            <div class="level-1" v-if="item.tabName">
                <div class="icon">
                    <img :src="item.tabIcon" alt="">
                </div>
                <div class="label">{{ item.tabName }}</div>
            </div>
            <div @click="menuClick(child)" class="level-2" :class="{'level-2-chose': child.isChose}" v-for="(child, childIndex) in item.children" :key="childIndex">
                <div class="icon">
                    <img v-show="child.tabIcon" :src="child.tabIcon" alt="">
                </div>
                <div class="label">{{ child.tabName }}</div>
            </div>
        </div>
    </div>
</template>

<style lang="less" scoped>
.level-menu {
    width: 100%;
    height: 100%;
    overflow: auto;
}
.menu-item { 
    width: 100%;
    box-sizing: border-box;
    .level-1 {
        width: 100%;
        height: 50px;
        display: flex;
        align-items: center;
        padding: 0 10px;
        cursor: pointer;
        box-sizing: border-box;
        .icon {
            width: 20px;
            height: 20px;
            margin-right: 10px;
            img {
                width: 100%;
                height: 100%;
            }
        }
        .label {
            font-size: 16px;
            font-weight: bold;
        }
    }
    .level-2 {
        width: 100%;
        height: 40px;
        display: flex;
        align-items: center;
        padding: 0 10px;
        cursor: pointer;
        box-sizing: border-box;
        .icon {
            width: 20px;
            height: 20px;
            margin-right: 10px;
            img {
                width: 100%;
                height: 100%;
            }
        }
        &:hover {
            background-color: rgba(255,245,240);
            color: rgb(255,128,64);
        }
        .label {
            font-size: 14px;
        }
    }
    .level-2-chose {
        position: relative;
        background-color: rgba(255,245,240);
        color: rgb(255,128,64);
        &::before {
            content: '';
            position: absolute;
            left: 0;
            top: 0;
            width: 4px;
            height: 100%;
            background-color: rgb(255,128,64);
        }
    }
}
</style>